// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

const { debug } = require("console");

const _db = uniCloud.database();
const db = uniCloud.databaseForJQL()
const dbCmd = db.command
module.exports = {
	_before: function() { // 通用预处理器
// this.param = this.getParam();
		try {
			this.param = JSON.parse(this.getHttpInfo().body);
			console.log("_before", JSON.stringify(this.param));
		} catch (e) {
			console.log("获取参数异常:", JSON.stringify(e));
		}
	},
	
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	getGameRankById: async function(param) {
		if (!this.param) {
			if (param) {
				this.param = param;
			}
		}
		const day = new Date();
		let today = "" + day.getFullYear() + (day.getMonth() + 1) + day.getDate();
		let maxScore = 1000;
		if(this.param.gameid == '676937b9ee97ef7195322ee3'){
			maxScore = 10000;
		}
		let userInfo = await db.collection("guagua-userscore-new,open_user").where({
			datetime: Number(today),
			gameid:this.param.gameid,
			addscore: dbCmd.gt(maxScore),
			scoretype:0
		}).field('_id,userid,addscore as userscore').limit(500).orderBy(
			'userscore', 'desc').get();
		for (var index = 0; index < userInfo.data.length; index++) {
			if(userInfo.data[index].userid && userInfo.data[index].userid.length > 0){
				userInfo.data[index].avatar = userInfo.data[index].userid[0].avatar;
				userInfo.data[index].nickname = userInfo.data[index].userid[0].nickname;
				userInfo.data[index].scoretype = userInfo.data[index].userid[0]._id;
				delete userInfo.data[index].userid;
			}
		}
		if(userInfo && userInfo.data &&  userInfo.data.length>100){
			const data = userInfo.data.slice(0,100);
			userInfo.data = data;
			return userInfo;
		}else{
			return userInfo;
		}
	},
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	getRankByDay: async function(param) {
		const day = new Date();
		let today = "" + day.getFullYear() + (day.getMonth() + 1) + day.getDate();
		let userInfo = await db.collection("open_user").where({
			todayscoredate: today,
			todayscore: dbCmd.gt(1000)
		}).field('_id,avatar,nickname,updateuserdate,todayscore as userscore').limit(1000).orderBy(
			'userscore', 'desc').get();
			// return userInfo;
		// userInfo.data.data
		if(userInfo && userInfo.data &&  userInfo.data.length>100){
			const data = userInfo.data.slice(0,100);
			userInfo.data = data;
			return userInfo;
		}else{
			return userInfo;
		}
		
	},

	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	getRankByTotal: async function(param) {
		let userInfo = await db.collection("open_user").where({
			score: dbCmd.gt(200000)
		}).field('_id,avatar,nickname,updateuserdate,score as userscore').limit(1000).orderBy('userscore',
			'desc').get();
			// return userInfo;
		if(userInfo && userInfo.data &&  userInfo.data.length>100){
			const data = userInfo.data.slice(0,100);
			userInfo.data = data;
			return userInfo;
		}else{
			return userInfo;
		}
	},


}